﻿using AnlianCommon.Common.Domains.BankCustomers;
using AnlianCommon.Common.Domains.OrganizationalUnits;
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepSelles
{
    public class SheepSellQueryHandler : IRequestHandler<SheepSellQueryCommand, ResultPage<SheepSellQueryDto>>
    {
        private readonly IBaseRepository<SheepSell> _sheepSellRepository;
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<Cottage> _cottageRepository;
        private readonly IBaseRepository<Field> _fieldRepository;
        private readonly IBaseRepository<Flat> _flatRepository;
        private readonly IBaseRepository<Department> _departmentRepository;

        public SheepSellQueryHandler(IBaseRepository<SheepSell> sheepSellRepository, IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<Cottage> cottageRepository, IBaseRepository<Field> fieldRepository, IBaseRepository<Flat> flatRepository, IBaseRepository<Department> departmentRepository)
        {
            _sheepSellRepository = sheepSellRepository;
            _sheepArchivesRepository = sheepArchivesRepository;
            _cottageRepository = cottageRepository;
            _fieldRepository = fieldRepository;
            _flatRepository = flatRepository;
            _departmentRepository = departmentRepository;
        }

        public async Task<ResultPage<SheepSellQueryDto>> Handle(SheepSellQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepSellQueryDto>() { TotalCount = 0, PagedCount = 0 };
            try
            {
                var sheepSell = await (from a in await _sheepSellRepository.GetAll()
                                       join b in await _sheepArchivesRepository.GetAll() on a.RecordAuricle equals b.RecordAuricle
                                       join e in await _departmentRepository.GetAll() on a.DepartmentId equals e.DepartmentId
                                       where a.IsDeleted == false
                                       select new SheepSellQueryDto()
                                       {
                                           SellId = a.SellId,
                                           RecordAuricle = a.RecordAuricle,
                                           SellTime = a.SellTime,
                                           ClientId = a.ClientId,
                                           Salesman = a.Salesman,
                                           Stash = a.Stash,
                                           DeliveryAddress = a.DeliveryAddress,
                                           DocumentNumber = a.DocumentNumber,
                                           ClosingPrice = a.ClosingPrice,
                                           FixPrice = a.FixPrice,
                                           Discount = a.Discount,
                                           DepartmentId = e.DepartmentId,
                                           DepartmentName = e.DepartmentName,
                                           ReviewStatus = a.ReviewStatus,
                                           CreatedOwnerId = a.CreatedOwnerId,
                                           CreatedTime = a.CreatedTime,
                                           UpdatedOwnerId = a.UpdatedOwnerId,
                                           UpdatedTime = a.UpdatedTime,
                                       }).ToListAsync();
                if (request.StartSellTime != null)
                {
                    sheepSell = sheepSell.Where(c => c.SellTime >= request.StartSellTime).ToList();
                }
                if (request.EndSellTime != null)
                {
                    sheepSell = sheepSell.Where(c => c.SellTime <= request.EndSellTime).ToList();
                }
                if (request.ClientId > 0)
                {
                    sheepSell = sheepSell.Where(c => c.ClientId == request.ClientId).ToList();
                }
                if (!string.IsNullOrEmpty(request.RecordAuricle))
                {
                    sheepSell = sheepSell.Where(c => c.RecordAuricle == request.RecordAuricle).ToList();
                }
                if (!string.IsNullOrEmpty(request.DocumentNumber))
                {
                    sheepSell = sheepSell.Where(c => c.DocumentNumber == request.DocumentNumber).ToList();
                }
                if (request.DepartmentId > 0)
                {
                    sheepSell = sheepSell.Where(c => c.DepartmentId == request.DepartmentId).ToList();
                }
                if (!string.IsNullOrEmpty(request.DeliveryAddress))
                {
                    sheepSell = sheepSell.Where(c => c.DeliveryAddress == request.DeliveryAddress).ToList();
                }
                if (!string.IsNullOrEmpty(request.Salesman))
                {
                    sheepSell = sheepSell.Where(c => c.Salesman == request.Salesman).ToList();
                }
                if (request.ReviewStatus > 0)
                {
                    sheepSell = sheepSell.Where(c => c.ReviewStatus == request.ReviewStatus).ToList();
                }
                result.TotalCount = sheepSell.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                sheepSell = sheepSell.OrderBy(c => c.SellId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheepSell;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
